/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.stop.risk.data.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.stop.risk.base.entity.LinePollMonitorSiteEntity;
import com.hopes.bizz.stop.risk.base.service.LinePollMonitorSiteService;
import com.hopes.bizz.stop.risk.base.vo.LinePollMonitorSiteDataVO;
import com.hopes.bizz.stop.risk.data.entity.LineOmRtDataEntity;
import com.hopes.bizz.stop.risk.data.service.LineOmRtDataService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 实时监控数据
 *
 * @author pig
 * @date 2024-02-22 10:00:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rtData")
@Tag(description = "rtData", name = "实时监控数据管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class LineOmRtDataController {

    private final LineOmRtDataService lineOmRtDataService;
    private final LinePollMonitorSiteService linePollMonitorSiteService;

    /**
     * 分页查询
     *
     * @param page         分页对象
     * @param lineOmRtData 实时监控数据
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('data_rtData_view')")
    public R getlineOmRtDataPage(Page page, LineOmRtDataEntity lineOmRtData) {
        LambdaQueryWrapper<LineOmRtDataEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(lineOmRtDataService.page(page, wrapper));
    }

    @Operation(summary = "列表查询", description = "列表查询")
    @GetMapping("/selectAllData")
    @PreAuthorize("@pms.hasPermission('data_rtData_view')")
    public R getlineOmRtDataList(@RequestParam(value = "id",required = false) Long id) {
        List<LinePollMonitorSiteEntity> siteEntities = linePollMonitorSiteService.lambdaQuery().eq(ObjectUtil.isNotNull(id), LinePollMonitorSiteEntity::getId, id).list();
        ArrayList<LinePollMonitorSiteDataVO> linePollMonitorSiteDataVOS = new ArrayList<>();
        for (LinePollMonitorSiteEntity siteEntity : siteEntities) {
            LinePollMonitorSiteDataVO linePollMonitorSiteDataVO = new LinePollMonitorSiteDataVO();
            BeanUtils.copyProperties(siteEntity,linePollMonitorSiteDataVO);
            Long siteEntityId = siteEntity.getId();
            linePollMonitorSiteDataVO.setRtDataList(lineOmRtDataService.lambdaQuery().eq(LineOmRtDataEntity::getMonitorSiteId,siteEntityId).list());
            linePollMonitorSiteDataVOS.add(linePollMonitorSiteDataVO);
        }
        return R.ok(linePollMonitorSiteDataVOS);
    }


    /**
     * 通过id查询实时监控数据
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('data_rtData_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(lineOmRtDataService.getById(id));
    }

    /**
     * 新增实时监控数据
     *
     * @param lineOmRtData 实时监控数据
     * @return R
     */
    @Operation(summary = "新增实时监控数据", description = "新增实时监控数据")
    @SysLog("新增实时监控数据")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('data_rtData_add')")
    public R save(@RequestBody LineOmRtDataEntity lineOmRtData) {
        return R.ok(lineOmRtDataService.save(lineOmRtData));
    }

    /**
     * 修改实时监控数据
     *
     * @param lineOmRtData 实时监控数据
     * @return R
     */
    @Operation(summary = "修改实时监控数据", description = "修改实时监控数据")
    @SysLog("修改实时监控数据")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('data_rtData_edit')")
    public R updateById(@RequestBody LineOmRtDataEntity lineOmRtData) {
        return R.ok(lineOmRtDataService.updateById(lineOmRtData));
    }

    /**
     * 通过id删除实时监控数据
     *
     * @param ids 列表
     * @return R
     */
    @Operation(summary = "通过id删除实时监控数据", description = "通过id删除实时监控数据")
    @SysLog("通过id删除实时监控数据")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('data_rtData_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(lineOmRtDataService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param lineOmRtData 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('data_rtData_export')")
    public List<LineOmRtDataEntity> export(LineOmRtDataEntity lineOmRtData) {
        return lineOmRtDataService.list(Wrappers.query(lineOmRtData));
    }
}
